﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CalculatorFrm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            bool b = CheckValue(textBox1.Text, comboBox1.Text, textBox2.Text);
            if (false == b)
            {
                return;
            }
            textBox3.Text = Calculate(textBox1.Text, comboBox1.Text, textBox2.Text).ToString();
        }

        public double Calculate(string number1, string ope, string number2)
        {
            bool b = false;
            double n1 = GetNumber(number1, ref b);
            double n2 = GetNumber(number2, ref b);
            switch (ope)
            {
                case "+":
                    return n1 + n2;
                case "-":
                    return n1 - n2;
                case "*":
                    return n1 * n2;
                case "/":
                    return n1 / n2;
                default:
                    break;
            }
            return 0;
        }

        /// <summary>
        /// 检查输入的数据
        /// </summary>
        /// <param name="number1">第一个数字</param>
        /// <param name="ope">运算符</param>
        /// <param name="number2">第二个数字</param>
        /// <returns>数据是否合法</returns>
        public bool CheckValue(string number1, string ope, string number2)
        {
            if ("" == ope)
            {
                MessageBox.Show("请选择运算符");
                return false;
            }
            bool b = false;
            GetNumber(number1, ref b);
            if (false == b)
            {
                MessageBox.Show("第一个参数不是合法的数字！");
                return false;
            }
            GetNumber(number2, ref b);
            if (false == b)
            {
                MessageBox.Show("第二个参数不是合法的数字！");
                return false;
            }
            if ("/" == ope && "0" == number2)
            {
                MessageBox.Show("除法运算时不能这样操作！");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 检查字符串是否为合法数字
        /// </summary>
        /// <param name="str">输入的字符串</param>
        /// <param name="b">返回值，是否合法</param>
        /// <returns>合法即返回转换后的值</returns>
        public double GetNumber(string str, ref bool b)
        {
            double number = 0;
            if (double.TryParse(str, out number))
            {
                b = true;
                return number;
            }
            else
            {
                b = false;
                return 0;
            }
        }

        public string number1;
        //public string number2;
        public string ope = "";

        private void button21_Click(object sender, EventArgs e)
        {
            //点击等号
            
            //检查数据

            bool b = CheckValue(number1, ope, textBox4.Text);
            if (false == b)
            {
                return;
            }
            textBox5.AppendText(Environment.NewLine + number1 + ope + textBox4.Text);
            textBox4.Text = Calculate(number1, ope, textBox4.Text).ToString();
            textBox5.Text = textBox5.Text + "=" + textBox4.Text;
            n2Cls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DealNumber("7");
        }

        public bool n2Cls = false;

        public void DealNumber(string arg)
        {
            if (ope == "")
            {
                DealTxt(arg);
            }
            else
            {
                if (false == n2Cls)
                {
                    textBox4.Text = "";
                    n2Cls = true;
                }
                DealTxt(arg);
            }
            
        }

        public void DealTxt(string arg)
        {
            if ("+/-" == arg)
            {
                bool b = false;
                double n1 = GetNumber(textBox4.Text, ref b);
                textBox4.Text = ((-1) * n1).ToString();
            }
            else if ("." == arg)
            {
                if (textBox4.Text.Contains("."))
                {
                    return;
                }
                else
                {
                    textBox4.Text = textBox4.Text + arg;
                }
            }
            else
            {
                textBox4.Text = textBox4.Text + arg;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DealNumber("8");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DealNumber("9");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DealNumber("4");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DealNumber("5");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            DealNumber("6");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            DealNumber("1");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            DealNumber("2");
        }

        private void button9_Click(object sender, EventArgs e)
        {
            DealNumber("3");
        }

        private void button10_Click(object sender, EventArgs e)
        {
            DealNumber("+/-");
        }

        private void button11_Click(object sender, EventArgs e)
        {
            DealNumber("0");
        }

        private void button12_Click(object sender, EventArgs e)
        {
            DealNumber(".");
        }

        private void button13_Click(object sender, EventArgs e)
        {
            DealOpe("+");
        }

        public void DealOpe(string ope1)
        {
            if ("" == textBox4.Text)
            {
                textBox4.Text = "0";
            }
            number1 = textBox4.Text;
            ope = ope1;
            textBox4.Text = "";
            n2Cls = false;
        }

        private void button14_Click(object sender, EventArgs e)
        {
            DealOpe("-");
        }

        private void button15_Click(object sender, EventArgs e)
        {
            DealOpe("*");
        }

        private void button16_Click(object sender, EventArgs e)
        {
            DealOpe("/");
        }

        private void button19_Click(object sender, EventArgs e)
        {
            double number = 0;
            double.TryParse(textBox4.Text, out number);
            textBox4.Text = (number * number).ToString();
            n2Cls = false;
        }

        private void button20_Click(object sender, EventArgs e)
        {
            double number = 0;
            double.TryParse(textBox4.Text, out number);
            textBox4.Text = Math.Sqrt(number).ToString();
            n2Cls = false;
        }

        private void button22_Click(object sender, EventArgs e)
        {
            ope = "";
            textBox4.Text = "";
            number1 = "";
        }
    }
}
